<?php
/**
 * 构建各种查询语句
 *
 */

namespace ulue\core\db\base\query;

use Ulue,
    Debug;

abstract class AbstractSqlBuilder implements InterfaceSqlBuilder
{
    static protected $driver='';

    /**
     * Property query.
     * @var  AbstractQuery
     */
    static public $query = null;

    /**
     * @param array $options
     * @example
     * [
     *  'condition' => 'xxx',
     *  'select' => ''
     *  ...
     * ]
     */
    public function __construct(array $options=[])
    {

    }

    /**
     * build
     *
     * @return  string
     */
    public static function build()
    {
        $args = func_get_args();

        $sql = array();

        foreach ($args as $arg) {
            if ($arg === '' || $arg === null || $arg === false)
            {
                continue;
            }

            $sql[] = $arg;
        }

        return implode(' ', $sql);
    }


    /**
     * getQuery
     * @param bool $new
     * @throws \DMissArgumentException
     * @return  AbstractQuery
     */
    public static function getQuery($new = false)
    {
        if (!static::$query || $new) {
            if (! $driver = static::$driver)
            {
                throw new \DMissArgumentException('please setting static property $driver -- current database driver name !');
            }

            $class = sprintf('\ulue\core\db\%s\query\Query',$driver);

            static::$query = new $class;
        }

        return static::$query;
    }

}